﻿#if NET461_OR_GREATER || NET5_0_OR_GREATER

using AutoCommunication.Base;
using AutoCommunication.Clients;
using AutoCommunication.Enums;
using AutoCommunication.Helper;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AutoCommunication.Wpf
{
	/// <summary>
	/// Window1.xaml 的交互逻辑
	/// </summary>
	public partial class Window_Configure : Window
	{
		public Window_Configure()
		{
			InitializeComponent();
			var sourse = typeof(IDevice).Assembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IDevice)) && !x.IsAbstract).ToList();
			comboBox_AllDeviceType.DisplayMemberPath = "Name";
			comboBox_AllDeviceType.ItemsSource = sourse;
			listBox_Devs.ItemsSource = devlist;
			listBox_Devs.DisplayMemberPath = "Name";
		}

		private ObservableCollection<IDevice> devlist = new();

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			var dev = CreateDevic(comboBox_AllDeviceType.SelectedItem as Type);
			if (dev == null)
			{
				MessageBox.Show("这个设备还没实现哦,请期待..");
				return;
			}
			if (dev == null)
			{
				MessageBox.Show("大哥,请先选择一个要添加的设备");
				return;
			}
			int c = 0;
			var name = dev.Name.Norm() + c;
			while (!(devlist.FirstOrDefault(x => x.Name.Norm() == name.Norm()) == null))
			{
				c++;
				name = dev.Name + c;
			}
			dev.Name = name.Norm();
			var res = dev.Init1();
			if (res.IsSucceed)
			{
				devlist.Add(dev);
				//propertyGrid1.SelectedObject = devlist.Last();
				//listBox_Devs.DataSource = null;
				//listBox_Devs.DataSource = devlist;
				//listBox_Devs.DisplayMember = "Name";
				//listBox_Devs.SelectedItem = devlist.Last();
			}
			else
			{
				res.ShowMessageBox();
			}
		}

		private IDevice CreateDevic(Type type)//CreateDevic By Type
		{
			IDevice dev = null;
			if (type == typeof(DevSiemens))
			{
				dev = new DevSiemens()
				{
					AllVarable = new List<VarableSiemens>()
					{
						new VarableSiemens("mb0",DataTypeEnum.Byte) { Name = "mb0", DataChangeEnable = false },
						new VarableSiemens("mb3.5", DataTypeEnum.Bool) { Name = "mb3.5" ,DataChangeEnable=true},
						new VarableSiemens("db3.dbw2", DataTypeEnum.UInt16){ Name ="int"},
						new VarableSiemens("md23", DataTypeEnum.Float),
					   new VarableSiemens("db3.dbw14", DataTypeEnum.String){ Name ="string",Bytelength=20},
						new VarableSiemens("db3.dbw270", DataTypeEnum.Wstring){ Name ="wstring",Bytelength=500},
					},
				};
			}
			else if (type == typeof(DevModbusRtu))
			{
				dev = new DevModbusRtu()
				{
					AllVarable = new List<VarableModbus>()
				   {
				  new VarableModbus("00001", DataTypeEnum.Bool){ Name="00001"},
				  new VarableModbus("00002", DataTypeEnum.Bool){ Name="00002"},
				   new VarableModbus("40001", DataTypeEnum.Int16){ Name="40001"},
                 //  new VarableModbus("50001", DataTypeEnum.Int16){ Name="400033"},//错误的地址
                  new VarableModbus("40004", DataTypeEnum.Float){ Name="40004"}
				   }
				};
			}
			else if (type == typeof(DevModbusTcp))
			{
				dev = new DevModbusTcp()
				{
					AllVarable = new List<VarableModbus>()
				   {
				   new VarableModbus("00001", DataTypeEnum.Bool){ Name="00001"},
				   new VarableModbus("00002", DataTypeEnum.Bool){ Name="00002"},
				   new VarableModbus("40001", DataTypeEnum.Int16){ Name="40001"},
                 //  new VarableModbus("50001", DataTypeEnum.Int16){ Name="400033"},
                  new VarableModbus("40004", DataTypeEnum.Float){ Name="40004"}
				   }
				};
			}
			else if (type == typeof(DevMitsubishi))
			{
				//  dev = new MitsubishiClient();
			}
			else
			{
				MessageBox.Show("类型不对");
			}
			return dev;
		}

		private void Button_Click_1(object sender, RoutedEventArgs e)
		{
			OpenFileDialog o = new OpenFileDialog();
			o.Filter = "setting.json|*.Json|all|*.*";
			if (o.ShowDialog() == true)
			{
				var res = Auto.LoadByJson(o.FileName);

				if (res.IsSucceed)
				{
					devlist = res.Value;
					listBox_Devs.ItemsSource = devlist;
					listBox_Devs.DisplayMemberPath = "Name";
				}
				else
				{
					res.ShowMessageBox();
				}
			}
		}

		private void Button_Click_2(object sender, RoutedEventArgs e)
		{
			var x = devlist.Remove((listBox_Devs.SelectedItem as IDevice));
			if (devlist.Any())
			{
				listBox_Devs.SelectedItem = devlist.Last();
			}
		}

		private void Button_Click_3(object sender, RoutedEventArgs e)
		{
			devlist.SaveAsJson().ShowMessageBox();
		}

		private void Button_Click_4(object sender, RoutedEventArgs e)
		{
			if (listBox_Devs.SelectedItem != null)
			{
				((IDevice)listBox_Devs.SelectedItem).ShowByWinform();
			}
			else
			{
				MessageBox.Show("请新建或者选择一个要测试的设备");
			}
		}

		private void listBox_Devs_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			devDetial.ItemsSource = e.AddedItems;
		}

		private void listBox_Devs_Selected(object sender, RoutedEventArgs e)
		{
			;
		}

		// public Dictionary<string, IDevice> Devs { get; set; }
	}
}

#endif